package com.mew.lobby.handler.user;

import com.mew.common.dc.redis.UserProfileRedisService;
import com.mew.common.entity.user.User;
import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.bo.MewSession;
import com.mew.common.rest.user.UserDetailedInfoResponseMessage;
import com.mew.lobby.annotation.LobbyHandler;
import com.mew.lobby.message.response.UserInfoResponseMessage;
import com.mew.lobby.net.LobbyHandlerMapCmdType;
import com.mew.lobby.net.MewHandler;
import com.mew.lobby.provide.UserFeignService;
import com.zebra.remoting.config.EmptyMessage;
import com.zebra.remoting.protocol.ZebraCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**用户信息
 * @author dylin
 * @create 2021/1/26 0026
 */
@Service
@Slf4j
@LobbyHandler(LobbyHandlerMapCmdType.UserInfo)
public class UserInfoHandler implements MewHandler<EmptyMessage> {
    @Autowired
    private UserProfileRedisService profileService;
    @Autowired
    private UserFeignService userFeignService;
    @Override
    public void handle(ZebraCommand<EmptyMessage> zebraCommand, MewSession session) {
        long userid = zebraCommand.getUserId();
        User user = profileService.getRedisUserById(userid);
        if(user==null){
            zebraCommand.sendFailureCode(ResponseCode.LoginUn.code());
        }else{
            UserDetailedInfoResponseMessage ud=null;
            if(user.getInviter()!=null&&user.getInviter()>0){
                ud=userFeignService.getDetailedInfoByUid(session.uid, user.getInviter());
            }
            zebraCommand.sendMessage(UserInfoResponseMessage
                    .builder()
                    .tel(user.getTelNumber())
                    .inviter(null==ud?"":ud.getGid())
                    .signature(user.getSignature())
                    .build());
        }
    }


}
